package leetCode.sample;

public class MaxAreaSolution {

    public static void main(String[] args) {
        int[] height = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7};
        System.out.println(maxArea(height));
        System.out.println(maxArea2(height));

    }

    private static int maxArea2(int[] height) {
        if (height.length < 2) {
            return 0;
        }
        int maxArea = 0;
        int i = 0, j = height.length - 1;
        while (j > i) {
            int tmpArea = (j - i) * (height[i] > height[j] ? height[j] : height[i]);
            if (tmpArea > maxArea) {
                maxArea = tmpArea;
            }
            if (height[i] > height[j]) {
                j--;
            } else {
                i++;
            }
        }
        return maxArea;
    }

    private static int maxArea(int[] height) {
        if (height.length < 2) {
            return 0;
        }
        int maxArea = 0;
        for (int i = 0; i < height.length - 1; i++) {
            for (int j = i + 1; j < height.length; j++) {
                int minHeight = height[i] > height[j] ? height[j] : height[i];
                int tmpArea = (j - i) * minHeight;
                if (tmpArea > maxArea) {
                    maxArea = tmpArea;
                }
            }
        }
        return maxArea;
    }
}
